package cn.hchaojie.camp.week1.qa;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class Ex14MatrixSum2 {
	private static final double[][] MATRIX = {
			{1.5, 2, 3, 4},
			{2.3, 5, 8, 9},
			{3, 5.4, 7, 10}
		};
		
	private static final double[] RESULT = {6.8, 12.4, 18, 23};
	private static final double E = 0.001;
	
	@Test
	public void verify() {
		double[] sum = sumColumn(MATRIX);
		for (int i = 0; i < RESULT.length; i++) {
			assertTrue(Math.abs(sum[i] - RESULT[i]) < E);
		}
	}
	
	public static double[] sumColumn(double[][] matrix) {
		double[] result = new double[matrix[0].length];
		for (int j = 0; j < result.length; j++) {
			double sum = 0;
			for (int r = 0; r < matrix.length; r++) {
				sum += matrix[r][j];
			}
			
			result[j] = sum;
		}
		return result;
	}
}
